package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.pojo.Material;
import com.kindergarten.pojo.Notice;
import com.kindergarten.service.IMaterialService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.MMGridPageVoBean;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author bruce
 * @since 2022-10-26
 */
@Controller
@RequestMapping("/material-api")
public class MaterialController {

    @Autowired
    IMaterialService materialService;

    /**
     * 01-跳转到模板引擎页面
     * @return
     */
    @GetMapping("topage")
    public String topage() {
        return "material";
    }

    /**
     * 02-跳转到模板引擎页面
     * @return
     */
    @GetMapping("toaddpage")
    public String toaddpage() {
        return "materialAdd";
    }


    /**
     * 02-数据加载分页
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    public MMGridPageVoBean page(Material material) {
        MMGridPageVoBean pageVoBean = new MMGridPageVoBean();
        QueryWrapper<Material> queryWrapper = new QueryWrapper<Material>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("del",0); //删除不查询

        queryWrapper.like(material.getName() != null && !"".equals(material.getName()), "name", material.getName());
        queryWrapper.like(material.getFromplace() != null && !"".equals(material.getFromplace()), "fromplace", material.getFromplace());
        IPage<Material> page = materialService.page(new Page<Material>(material.getPage(), material.getLimit()), queryWrapper);
        pageVoBean.setRows(page.getRecords()); //每页数据
        pageVoBean.setTotal(page.getTotal()); //总条数
        return pageVoBean;
    }

    /**
     * 新增
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    public DataResults add(Material material){
        return DataResults.success(ResultCode.SUCCESS,materialService.save(material));
    }

    /**
     * 02-跳转到模板引擎页面
     * @return
     */
    @GetMapping("toupdatepage")
    public String toupdatepage(Integer id, Model model) {
        //数据回显用的
        Material material = materialService.getById(id);
        model.addAttribute("materialOject",material);
        return "materialUpdate";  //模板引擎页面
    }

    /**
     * 更新
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public DataResults update(Material material){
        return DataResults.success(ResultCode.SUCCESS,materialService.updateById(material));
    }

    /**
     * 删除
     * @return
     */
    @ResponseBody
    @DeleteMapping("delete/{id}")
    public DataResults delete(@PathVariable("id") Integer id){
        Material material=new Material();
        material.setId(id);
        material.setDel(1);
        return DataResults.success(ResultCode.SUCCESS,materialService.updateById(material));
    }
}
